﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using XeBus.BusinessObject;

namespace XeBus.DataAccess
{
    public class LoTrinhDAO
    {
        private ConnectData _conn;

        public LoTrinhDAO()
        {
            _conn = new ConnectData();
        }

        // Hàm thêm mới một lộ trình
        public void THEM_LO_TRINH(LoTrinh lt)
        {

            SqlParameter[] param = new SqlParameter[3];

            param[0] = new SqlParameter("P_TEN_LO_TRINH", SqlDbType.NVarChar);
            param[0].Value = lt.Ten_Lo_Trinh;

            param[1] = new SqlParameter("P_HUONG_DI", SqlDbType.NVarChar);
            param[1].Value = lt.Huong_Di;

            param[2] = new SqlParameter("P_MA_TUYEN", SqlDbType.Int);
            if (lt.Ma_Tuyen.Length > 0)
                param[2].Value = lt.Ma_Tuyen;
            else
                param[2].Value = DBNull.Value;

            _conn.ExecuteNonQuery("THEM_LOTRINH", param);

        }

        public DataTable LoTrinh()
        {
            return _conn.ExecuteSelectQuery("SELECT * FROM LoTrinh", null);
        }

        public DataTable LoTrinh(string ma_lo_trinh)
        {
            return _conn.ExecuteSelectQuery("SELECT * FROM LoTrinh WHERE Ma_Tuyen=" + ma_lo_trinh + "", null);
        }

        public DataTable LoTrinhTheoID(string id)
        {
            return _conn.ExecuteSelectQuery("SELECT * FROM LoTrinh WHERE Ma_Lo_Trinh=" + id + "", null);
        }

        public void SUA_LO_TRINH(LoTrinh lt)
        {
            SqlParameter[] param = new SqlParameter[4];

            param[0] = new SqlParameter("P_TEN_LO_TRINH", SqlDbType.NVarChar);
            param[0].Value = lt.Ten_Lo_Trinh;

            param[1] = new SqlParameter("P_HUONG_DI", SqlDbType.NVarChar);
            param[1].Value = lt.Huong_Di;

            param[2] = new SqlParameter("P_MA_LO_TRINH", SqlDbType.Int);
            if (lt.Ma_Lo_Trinh.Length > 0)
                param[2].Value = lt.Ma_Lo_Trinh;
            else
                param[2].Value = DBNull.Value;

            param[3] = new SqlParameter("P_MA_TUYEN", SqlDbType.Int);
            if (lt.Ma_Tuyen.Length > 0)
                param[3].Value = lt.Ma_Tuyen;
            else
                param[3].Value = DBNull.Value;

            _conn.ExecuteNonQuery("SUA_LOTRINH", param);
        }

        public void XOA(string id)
        {
            _conn.ExecuteNonQueryString("DELETE FROM LoTrinh WHERE Ma_Lo_Trinh=" + id + ";");
        }

        public DataTable Ma_Tuyen()
        {
            return _conn.ExecuteSelectQuery("SELECT Ma_Tuyen FROM TuyenXeBuyt",null);
        }
    }
}
